from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.paginator import Paginator
from .ser import *


# Create your views here.

class NewsM(APIView):
    def post(self, request):
        ser = NewsSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code':20010})

    def get(self, request):
        cpage = request.GET.get('cpage')
        news = News.objects.all()
        p = Paginator(news, 2)
        newss = p.page(cpage)
        ser = NewsSer(newss, many=True)
        return Response({'code': 200, 'list': ser.data, 'tpage': p.num_pages})


class XWM(APIView):
    def post(self, request):
        ser = XWser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code':20010})

    def get(self, request):
        cpage = request.GET.get('cpage')
        xinw = Advertisement.objects.all()
        p = Paginator(xinw, 2)
        xinws = p.page(cpage)
        ser = XWser(xinws, many=True)
        return Response({'code': 200, 'list': ser.data, 'tpage': p.num_pages})
